package com.mills.gamebase.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mills.gamebase.dao.IRoleDAO;
import com.mills.gamebase.dao.IUserDAO;
import com.mills.gamebase.model.Role;
import com.mills.gamebase.model.User;
import com.mills.gamebase.service.Dict;
import com.mills.gamebase.service.SessionUtils;
import com.mills.gamebase.service.StatusMessage;

@Controller
public class UserController {
	
	@Autowired private IRoleDAO roleDAO;
	@Autowired private IUserDAO userDAO;
	@Autowired private Validator validator;
	
	@InitBinder
	protected void initBinder( ServletRequestDataBinder binder) {

	}

	@RequestMapping("/system/register.htm")
	public ModelAndView register (HttpServletRequest request) {
		ModelMap map = new ModelMap();
		map.addAttribute(Dict.MM_CONTENT, "system/register.jsp");
		
		SessionUtils.addStylesheet(request.getSession(), "system/register.css");
		
		map.addAttribute("user", new User());
		
		return new ModelAndView("index", map);
	}
	
	@RequestMapping("/system/perform_registration.htm")
	public ModelAndView perform_registration (User user, BindingResult result, HttpServletRequest request) {
		
		validator.validate(user, result);
		
		boolean usernameUniqe = true;
		if (user.getUsername() != null) {
			usernameUniqe = !userDAO.usernameExists(user.getUsername(), user.getId());
		}
		
		if (result.hasErrors() || !usernameUniqe) {
			ModelMap map = new ModelMap();
			map.addAttribute(Dict.MM_CONTENT, "system/register.jsp");
			SessionUtils.addStylesheet(request.getSession(), "system/register.css");
			
			map.addAttribute("user", user);
			if (!usernameUniqe) 
				map.addAttribute("unique", false);
			
			return new ModelAndView("index", map);
		}
		
		// Dem User die Rolle Dict.ROLE_STANDARD_USER zuweisen
		{
			Role standardUser = roleDAO.getByName(Dict.ROLE_STANDARD_USER);
			if (standardUser == null)
				SessionUtils.addError(request.getSession(), "Die Rolle '" + Dict.ROLE_STANDARD_USER + "' konnte nicht gefunden werden!");
			else {
				List<Role> roleList = new ArrayList<Role>();
				roleList.add(standardUser);
				user.setRoles(roleList);
			}
		}
		
		// Den User speichern
		StatusMessage msg = userDAO.save(user);
		if (msg.success()) {
			SessionUtils.addInfo(request.getSession(), "Sie wurden erfolgreich registriert.");
		} else {
			SessionUtils.addError(request.getSession(), "Bei der Registrierung ist ein Fehler aufgetreten.");
		}
		
		return new ModelAndView("redirect:start.htm");
	}

	@RequestMapping("/user/overview.htm")
	public ModelAndView overview (HttpServletRequest request) {
		ModelMap map = new ModelMap();
		map.addAttribute(Dict.MM_CONTENT, "user/overview.jsp");
		
		SessionUtils.addStylesheet(request.getSession(), "user/overview.css");
		
		map.addAttribute("users", userDAO.getAll(IUserDAO.PRELOAD_ROLES));
		
		return new ModelAndView("index", map);
	}
}
